var express = require('express');
var router = express.Router();

const { getParkingsById, updataAuditParkings, getParkings, addParkings, deleteParkings, deleteParkingsById,
    confirmChange, getAll, getListById, changeParkingState, backParking } = require('../service/parkingsService')

//渲染(出租人)
router.get('/getParkingsById', async function (req, res, next) {
    const data = await getParkingsById(req.query);
    res.send(data);
});

//渲染（管理员）
router.get('/getParkings', async function (req, res, next) {
    const data = await getParkings(req.query);
    res.send(data);
})

//增加
router.post('/addParkings', async function (req, res, next) {
    console.log('req.body', req.body);
    const parkings = req.body;
    const data = await addParkings(parkings);
    res.send(data);
})

//修改
router.post("/confirmChange", async function (req, res) {
    console.log("进入修改API");
    const { _id, address, number, property, cycle, price } = req.body;
    const data = await confirmChange({ _id, address, number, property, cycle, price });
    res.send(data);
})


//删除（管理员）
router.post('/deleteParkings', async function (req, res, next) {
    const _id = req.body;
    const data = await deleteParkings(_id);
    res.send(data);
})

//删除（出租人）
router.post('/deleteParkingsById', async function (req, res, next) {
    const _id = req.body;
    const data = await deleteParkingsById(_id);
    res.send(data);
})

//修改审核
router.post('/updataAuditParkings', async function (req, res, next) {
    const { _id, state } = req.body;
    console.log(11111, req.body);
    const data = await updataAuditParkings({ _id, state })
    res.send(data);
});


// 获取所有数据
router.get('/getAll', async function (req, res, next) {
    const code =req.query;
    const data = await getAll(code)
    // console.log(123,data)
    res.send(data);
});

// 通过Id获取
router.post('/getListById', async function (req, res, next) {
    const id = req.body
    const data = await getListById(id.id)
    res.send(data);
});


// 购买车位后修改状态
router.post('/changeParkingState', async function (req, res, next) {
    const id = req.body
    const data = await changeParkingState(id.id)
    res.send(data);
});


// 归还车辆
router.post('/backParking', async function (req, res, next) {
    const id = req.body
    const data = await backParking(id.id)
    res.send(data);
});

module.exports = router;
